export default {
	data() {
		return{
			date1: '',
			rules: {
				threshHold: [
					{pattern: /^([1-9][0-9]{0,20})$/, message: '请输入正确的监控阈值', trigger: 'blur' }
				]
			},
			form: {
				inquerySQLOne: '',
				inquerySQLTwo: '',
				threshHold: '',
				description: '',
				gameName:'',
				gameId:''
			},
			// 修改数据
			revampForm: {
				inquerySQLOne: '',
				inquerySQLTwo: '',
				threshHold: '',
				description: '',
				gameName:'',
				gameId:'',
				id: '',
			},
			gamelists:'',
			tableHeader: [
				{"prop":"id","label":"序列号",width:'100px'},
				{"prop":"gameName","label":"游戏名称",width:'140px'},
				{"prop":"gameId","label":"游戏ID",width:'100px'},
				{"prop":"inquerySQLOne","label":"监控语句 (服务器ID-最大值-最小值-玩家总数)",width:'500px'},
				{"prop":"inquerySQLTwo","label":"监控语句 (超出阀值玩家数)",width:'500px'},
				{"prop":"threshHold","label":"监控阈值",width:'100px'},
				{"prop":"description","label":"监控描述",width:'200px'},
			],
			//表数据
			tableData: [],
			//总条数
			total:0,
			//每页显示的条数
			pageSize:10,
			//当前页
			currentPage: 1,
			//请求地址
			SRC:'',
			//修改框显示变量
			revampShow: false,
			testTime:''
		} 
	},
	methods: {
		//添加
		addForm (form) {
			this.$refs[form].validate((valid) => {
				if (valid) {
					this.addData()
				}else {
					this.errorFun('请完善输入的值！');
					return false;
				}
			});
		},
		findForm(form){
			this.$refs[form].validate((valid) => {
				if (valid) {
					this.findData()
				}else {
					this.errorFun('请完善输入的值！');
					return false;
				}
			});
		},
		resetForm (formName) {
			this.$refs[formName].resetFields();
			this.form.gameId='';
			this.form.gameName='';
			this.revampForm.gameId='';
			this.revampForm.gameName='';
		},
		successFun(data) {
	      this.$message({
	        message: data,
	        type: 'success'
	      });
	    },
		errorFun(data) {
			this.$message.error(data);
		},
		//每页显示条数切换
		handleSizeChange(val) {
			console.log(val);
			this.pageSize = val
			this.getData(this.currentPage,this.pageSize)
		},
		//页码切换
		handleCurrentChange(val) {
			console.log(val);
			this.currentPage = val
			this.getData(this.currentPage,this.pageSize)
		},
		//初始化以及分页
		getData(currentPage,pageSize){
			this.tableData=[];
			this.$http.get(`${this.SRC}dailyreport/list.do?curPage=${currentPage}&pageSize=${pageSize}`).then(function(res){
	    			var data =  res.body
	    			console.log(data)
	    			if(parseInt(data.code) == 0){
	    				 this.currentPage = data.msg.curPage;
	    				 this.pageSize = data.msg.pageSize;
	    				 this.total = data.msg.totalSize;
	    				 var allData = data.msg.data;
	    				 this.tableData = this._pushData(allData)
	    			}else{
	    				this.errorFun(data.msg)
	    			}
	    		})
		},
		_pushData(arr){
			var newArr = []
			for(var i = 0,len = arr.length;i<len;i++){
				var obj = {
						gameName : arr[i].gameName,
						gameId : arr[i].gameId,
						id : arr[i].id,
						inquerySQLOne : arr[i].inquerySQLOne,
						inquerySQLTwo : arr[i].inquerySQLTwo,
						threshHold : arr[i].threshHold,
						description : arr[i].description
				}
				newArr.push(obj)
			}
			return newArr;
		},
		//添加配置
		addData(){
			var obj = {
	        		inquerySQLOne: this.form.inquerySQLOne,
	        		inquerySQLTwo: this.form.inquerySQLTwo,
					threshHold: this.form.threshHold,
					gameName: this.form.gameName,
					gameId: this.form.gameId,
					description: this.form.description
			}
			this.$http.post(`${this.SRC}dailyreport/add.do`,obj,{emulateJSON:true}).then(function(res){
				var data = res.body;
				if(parseInt(data["code"]) === 0){
					this.successFun(data["msg"]); //提示成功
					this.getData(this.currentPage,this.pageSize); //重新获取表格数据
					this.resetForm ('form')//清空文本框的内容
				}else{
					this.errorFun(data['msg'])
				}
    		})
		},
		findData(){
			var d = this.form;
			var obj = {
				gameId: d.gameId,
				gameName: d.gameName,
				inquerySQLOne: d.inquerySQLOne,
				inquerySQLTwo: d.inquerySQLTwo,
				threshHold: d.threshHold,
				description: d.description
			}
			var str = '';
			for(var i in obj){
				if(obj[i] !== ""){
					str += `${i}=${obj[i]}&`
				}
			}
			str = str.substring(0,str.length-1);
			console.log(`${this.SRC}dailyreport/list.do?curPage=${this.currentPage}&pageSize=${this.pageSize}&${str}`)
			this.$http.get(`${this.SRC}dailyreport/list.do?curPage=${this.currentPage}&pageSize=${this.pageSize}&${str}`).then(function(res){
				var data = res.body;
				console.log(data,data.msg.data.length == 0)
				if(parseInt(data['code']) === 0){
					this.tableData=[];
					if(data.msg.data.length == 0){
						this.errorFun('查询无结果！');
					}else{
						this.successFun('查询成功！');
					}
					this.currentPage = data.msg.curPage;
					this.pageSize = data.msg.pageSize;
					this.total = data.msg.totalSize;
					var allData = data.msg.data;
					this.tableData = this._pushData(allData)
				}else{
					this.errorFun(data['msg'])
				}
			})
			// console.log(
			// 	this.form.inquerySQLOne+'---'+this.form.inquerySQLTwo+'---'+this.form.threshHold+'---'+this.form.description+'---'+this.form.gameName+'---'+this.form.gameId)
		},
		// 删除数据
		handleDel(index, row) {
			this.$http.get(`${this.SRC}dailyreport/delete.do?id=${row.id}`).then(function(res){
				var data = res.body;
				if(parseInt(data['code']) === 0){
					this.successFun(data['msg']);
					this.getData(this.currentPage,this.pageSize); //重新获取表格数据
				}else{
					this.errorFun(data['msg'])
				}
			})
		},
		// 修改数据
		handleEdit(index, row) {
			console.log(index,row)
			this.revampForm.id = row.id;
			this.revampForm.gameId = row.gameId;
			this.revampForm.gameName = row.gameName;
			this.revampForm.inquerySQLOne = row.inquerySQLOne;
			this.revampForm.inquerySQLTwo = row.inquerySQLTwo;
			this.revampForm.threshHold = row.threshHold;
			this.revampForm.description = row.description;
			this.revampShow = true;
		},
		/*修改提交*/
		revampComit(formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {
					var d = this.revampForm;
					var obj = {
						id: d.id,
						inquerySQLOne:d.inquerySQLOne,
						inquerySQLTwo:d.inquerySQLTwo,
						gameName: d.gameName,
						gameId: d.gameId,
						threshHold: d.threshHold,
						description:d.description
					}
					this.$http.post(`${this.SRC}dailyreport/modify.do`,obj,{emulateJSON:true}).then(function(res){
						var data = res.body;
						if(parseInt(data['code']) === 0){
							this.successFun(data['msg']);
							this.getData(this.currentPage,this.pageSize); //重新获取表格数据
							this.revampShow = false;
						}else{
							this.errorFun(data['msg'])
						}
					})
				} else {
					return false;
				}
			});
		},
		//获取游戏列表
		getProject(){
			this.$http.get(`${this.SRC}project/list.do`).then(function(res){
    			var data =  res.body;
    			console.log(data)
    			if(parseInt(data.code) == 0){
    				this.gamelists= data.msg;
    			}else{
    				this.errorFun(data.msg)
    			}
    		})
		},
		// 游戏切换
		gameChange(val) {
			for(var i in this.gamelists){
				if(val === this.gamelists[i]['id']){
					this.form.gameName = this.gamelists[i]['projectName']
				}
			}
		},
		// 修改游戏切换
		gameRevamp(val) {
			for(var i in this.gamelists){
				if(val === this.gamelists[i]['id']){
					this.revampForm.gameName = this.gamelists[i]['projectName']
				}
			}
		},
		testChange(val){
			this.testTime = val
			console.log(this.testTime)
		},
		testDo(){
			this.$http.post(`${this.SRC}dailyreport/test.do`).then(function(res){
    			var data =  res.body;
    			console.log(data)
    			if(parseInt(data['code']) == 0){
    				this.successFun('操作成功！');
    			}else{
    				this.errorFun(data.msg)
    			}
    		})
		}
	},
	mounted(){
	    this.SRC = this.$store.getters.src;
		this.getData(this.currentPage,this.pageSize)
		this.getProject();
	}
}